// 导入defineStore
import { defineStore } from "pinia";

const useTodoStore = defineStore('todos',{
  state: () => ({
    // list:[
    //   { id: 1, name: '吃饭', done: false },
    //   { id: 2, name: '睡觉', done: true },
    //   { id: 3, name: '打豆豆', done: false }
    // ],
    list: JSON.parse(localStorage.getItem('todos')) || [] ,
    content: '',
    state:['全部','未完成','已完成'],
    active: '全部'
  }),
  actions: {
    changeDone(id){
      const res = this.list.find(item => item.id === id)
      res.done = !res.done
    },
    del(id){
      this.list = this.list.filter(item => item.id !== id)
    },
    add(){
      if(this.content.trim() === '') return  this.content = ''
      this.list.push({
        id: this.list.length + 1,
        name: this.content,
        done: false
      })
      this.content = ''
    },
    checkedAll(done){
      this.list.forEach(item => item.done = done)
    },
    clearComplate(){
      this.list =  this.list.filter(item => !item.done)
    },
    changeState(state){
      this.active = state
    }
  },
  getters: {
    leftCount(){
      return this.list.filter(item => !item.done).length
    },
    isAll(){
      return this.list.every(item => item.done)
    },
    showList(){
      if(this.active === '已完成'){
        return this.list.filter(item => item.done)
      }else if(this.active === '未完成'){
        return this.list.filter(item => !item.done)
      }else {
        return this.list
      }
    }
  }
})


// 导出数据函数
 export default useTodoStore